<?php

class My_Mem{
    public function get($key, $expired){
        $memcache = new Memcache;
        $memcache->connect('localhost',MEMCACHE_PORT) or die ("Could not connect");
        if($memcache->get("listKey_".BASE_URL.'_'.ENV)){
            $listKey = unserialize($memcache->get("listKey_".BASE_URL.'_'.ENV));
        }else{
            $listKey = array();
        }
        if(!in_array($key, $listKey)){
            $listKey[] = $key;
            $memcache->set("listKey_".BASE_URL.'_'.ENV, serialize($listKey), false, $expired);
        }
        return unserialize($memcache->get($key));
    }
    public function set($key,$data,$flag,$expired){
        $memcache = new Memcache;
        $memcache->connect('localhost',MEMCACHE_PORT) or die ("Could not connect");
        
        if($memcache->get($key)){
            $memcache->replace($key, $data, $flag, $expired);
        }else{
            $memcache->set($key, $data, $flag, $expired);
        }
        # luu lai danh sach key trong memcache
        if($memcache->get("listKey_".BASE_URL.'_'.ENV)){
            $listKey = unserialize($memcache->get("listKey_".BASE_URL.'_'.ENV));
        }else{
            $listKey = array();
        }
        if(!in_array($key, $listKey)){
            $listKey[] = $key;
            $memcache->set("listKey_".BASE_URL.'_'.ENV, serialize($listKey), $flag, $expired);
        }
    }
}